package com.example.student1.controller;

import com.example.student1.dao.StudentRepository;
import com.example.student1.domain.Student;
import com.example.student1.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@Slf4j
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    StudentRepository studentRepository;

    @RequestMapping("/student/list")
    public String list(HttpServletRequest httprequest, Model model)
    {

        HttpSession session = httprequest.getSession();
        if (session.getAttribute("userid")==null){
            return "redirect:/login/user";
        }
        List<Student> students = studentService.findAll();

        model.addAttribute("data",students);

        return "/student/list";
    }
    @RequestMapping("/student/list1")
    public String list1(HttpServletRequest httprequest, Model model)
    {

        HttpSession session = httprequest.getSession();
        if (session.getAttribute("userid")==null){
            return "redirect:/login/user";
        }
        List<Student> students = studentService.findAll();

        model.addAttribute("data",students);

        return "/student/list1";
    }

    @RequestMapping("/student/findAll")
    @ResponseBody
    public List<Student> find_All(){
        return studentRepository.findAll();
    }

    /*@GetMapping(value = "/student/{name}}")
    public String findbyname(String name,Model model1){
        List<Student> students=studentService.findbyname(name);
        List<Student> students1 = studentService.findAll();
        model1.addAttribute("data",students1);
        model1.addAttribute("data1",students);
        return "/student/list";
    }*/
   /* @RequestMapping(value = "/student/list1")
    public String list1(Model model2)
    {
        List<Student> students = studentService.findAll();
        model2.addAttribute("data2",students);
        return "/student/list1";
    }*/

    @RequestMapping (value = "/student/{name}}",method = RequestMethod.POST)
    public String findbyname(String name,Model model1){
        List<Student> students=studentService.findbyname(name);
        model1.addAttribute("data",students);
        log.info("name:{}",name);
        return "/student/list";
    }
    /*@GetMapping(value = "/student/list}")
    public String findbyname1(String name,Model model2) {
        List<Student> students1 = studentService.findAll();
        model2.addAttribute("data", students1);
        return "/student/list";
    }*/



}

